package com.king.template;

import com.king.common.template.DirectiveHandler;
import com.king.common.template.TemplateDirective;
import com.king.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * 本周热议
 */
@Component
public class HotsTemplate extends TemplateDirective {

    @Autowired
    RedisUtil redisUtil;

    @Override
    public String getName() {
        return "hots";
    }

    @Override
    public void execute(DirectiveHandler handler) throws Exception {
        String key = "week:rank";
        Map<String,Object> map;
        List<Map> results = new ArrayList<>();
        Set<ZSetOperations.TypedTuple> typedTuples = redisUtil.getZSetRank(key, 0, 6);
        for (ZSetOperations.TypedTuple item: typedTuples) {
            map= new HashMap<>();
            Object id = item.getValue();
            Double commentCount = item.getScore();
            map.put("id",id);
            map.put("commentCount",commentCount);
            map.put("title",redisUtil.hget( "post:rank:"+id,"post:title"));
            results.add(map);
        }

        handler.put(RESULTS,results).render();
    }
}
